import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * pdf转图片
 */
public class PdfToPicture {


    @Test
    public void test001() throws IOException {
//        File file = new File("c:\\test.pdf");
        File file = new File("C:\\Users\\fantai\\Desktop\\Tunight-计算摄影.pdf");

//　　　　PDDocument doc = PDDocument.save(file);
        PDDocument doc = new PDDocument();
        doc.save(file);
//　　　　PDFRenderer renderer = new PDFRenderer(doc);
        PDFRenderer renderer = new PDFRenderer(doc);
        int pageCount = doc.getNumberOfPages();
        for (int i = 0; i < pageCount; i++) {
//            BufferedImage image = renderer.renderImageWithDPI(i, 296);
            BufferedImage image = renderer.renderImage(i, 2.5f);
            ImageIO.write(image, "PNG", new File("D:\\data\\test" + i + ".png"));
        }
//　　　　for (int i = 0; i < pageCount; i++) {
//　　　　　　BufferedImage image = renderer.renderImageWithDPI(i, 296);
////          BufferedImage image = renderer.renderImage(i, 2.5f);
//　　　　　　ImageIO.write(image, "PNG", new File("C:\\pdfbox_image.png"));
//　　　　}

    }


}
